<?php

namespace business\elasticsearch\services;

use business\elasticsearch\repositories\NewsRepository;

class NewsService
{
    /**
     * 搜索列表
     * @param string $keyword
     * @param string $category_id
     * @param int $page
     * @param int $page_size
     * @return array
     */
    public function getList($keyword = '', $category_id = '', $page = 1, $page_size = 10)
    {
        $page = max($page, 1);
        $query = [];
        $keyword = trim($keyword);
        $category_id = trim($category_id);

        // 构建查询条件
        $this->builderQueryConds($query, $keyword, $category_id);
        $from = ($page - 1) * $page_size;
        $include = [];

        //高亮处理
        $highlight = [
            'pre_tags' => ['<em class="_high">'],
            'post_tags' => ['</em>'],
            "fields"=>[
                'title' => new \stdClass(),
                'tag' => new \stdClass(),
                'digest' => new \stdClass(),
                'content' => new \stdClass(),
            ]
        ];
        list($total, $data) = (new NewsRepository())->queryList($query, $include, $from, $page_size, $highlight);
        $result = ['resultNum' => $total, 'resultSet' => null];

        if (!empty($data)) {
            foreach ($data as $dv) {
                $tmp = $dv['_source'];
                if (!empty($dv['highlight'])) {
                    foreach ($dv['highlight'] as $key => $value) {
                        $tmp[$key . 'HL'] = isset($value[0]) ? $value[0] : '';
                    }
                }
                $tmp['showContent'] = empty($dc['digest']) ? $dc['content'] : $dc['digest'];
                $result['resultSet'][] = $tmp;
            }
        }

        return $result;
    }

    /**
     * 构建查询条件
     * @param array $query
     * @param string $keyword
     * @param string $category_id
     */
    public function builderQueryConds(&$query = [], $keyword = '', $category_id = '')
    {
        $bool = [];
        !empty($bool) && $query['bool'] = $bool;

        $bool = [];
        $boolKey = 0;

        !empty($category_id) && $this->builderCategoryQuery($bool, $boolKey, $category_id);

        !empty($bool) && $query['bool'] = $bool;
        // 构建关键词查询
        $this->builderKeywordQuery($query, $keyword);
    }

    /**
     * 构建关键词查询
     * @param array $query
     * @param $keyword
     */
    public function builderKeywordQuery(&$query = [], $keyword)
    {
        if (!empty($keyword)) {
            $query['bool']['should'] = [
                ['match' => ['title' => ['query' => $keyword, 'boost' => 10]]],
                ['match' => ['content' => ['query' => $keyword]]],
                ['match' => ['tag' => ['query' => $keyword, 'boost' => 10]]],
                ['match' => ['digest' => ['query' => $keyword]]],
            ];

            $query['bool']['minimum_should_match'] = 1;
        }
    }

    /**
     * 构建分类id查询
     * @param array $bool
     * @param $boolKey
     * @param $category_id
     */
    protected function builderCategoryQuery(&$bool = [], &$boolKey, $category_id)
    {
        if (!empty($category_id)) {
            $bool['filter']['bool']['must'][$boolKey]['bool']['must'][] = [
                'term' => [
                    'categoryId' => $category_id
                ]
            ];
            $boolKey++;
        }
    }



}